package session.stateless.SCM;

import entity.SCM.Batch;
import entity.SCM.ProductBalance;
import entity.SCM.RawMaterial;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class BatchBean implements BatchBeanLocal {
    
    @PersistenceContext()
    EntityManager em;

    @Override
    public Long createProductBatch(String productType, Date expiryDate, Long quantity, String location) {
        
        Batch batch = new Batch();
        
        ProductBalance product = em.find(ProductBalance.class, productType); 
        if(product == null){
            System.out.println("Product Not Exist!");
            return null;
        }
        Date batchDate = new Date();
        batch.setBatchType(Batch.BatchType.Product);
        batch.setBatchDate(batchDate);
        batch.setExpiryDate(expiryDate);
        batch.setLocation(location);
        batch.setQuantity(quantity);
        batch.setProduct(product);
        em.persist(batch);
        em.flush();
        
        long a = product.getBalance().longValue();
        long b = quantity.longValue();
        long sum = a + b;
        product.setBalance(new Long(sum));
        em.flush();
        
        return batch.getId();
    }

    @Override
    public List<Batch> getProductBatch() {
        
        Query q = em.createQuery("SELECT c FROM Batch c WHERE c.batchType = :batchType");
        q.setParameter("batchType", Batch.BatchType.Product);
        
        return q.getResultList();
    }

    @Override
    public Long createMaterialBatch(String materialType, Date expiryDate, Long quantity, String location) {
        Batch batch = new Batch();
        
        RawMaterial rawMaterial = em.find(RawMaterial.class, materialType); 
        if(rawMaterial == null){
            System.out.println("Raw Material Not Exist!");
            return null;
        }
        
        Date batchDate = new Date();
        batch.setBatchType(Batch.BatchType.Material);
        batch.setBatchDate(batchDate);
        batch.setExpiryDate(expiryDate);
        batch.setLocation(location);
        batch.setQuantity(quantity);
        batch.setMaterial(rawMaterial);
        em.persist(batch);
        em.flush();
        
        long a = rawMaterial.getBalance().longValue();
        long b = quantity.longValue();
        long sum = a + b;
        rawMaterial.setBalance(new Long(sum));
        em.flush();
        
        return batch.getId();
    }

    @Override
    public List<Batch> getMaterialBatch() {
        
        Query q = em.createQuery("SELECT c FROM Batch c WHERE c.batchType = :batchType");
        q.setParameter("batchType", Batch.BatchType.Material);
        
        return q.getResultList();
    }

    @Override
    public List<ProductBalance> getProduct() {
        
        Query q = em.createQuery("SELECT c FROM Product c");
        return q.getResultList();
    }

    @Override
    public List<RawMaterial> getMaterial() {
        
        Query q = em.createQuery("SELECT c FROM RawMaterial c");
        return q.getResultList();
    }


    
}
